package com.sam.db;

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class DatabaseLog extends JFrame
{
	
	static Connection connection = null;

	String driverName = "com.mysql.jdbc.Driver"; // for MySql
	String uri = "jdbc:mysql://localhost:3306/sam"; // for Mysql
	String username = "root"; // You should modify this.
	String password = ""; // You should modify this.
	String tablename=null;
    public DatabaseLog()
    {
    }
    
    @SuppressWarnings("unchecked")
	public void showDatabaseLog(String tablename){
    	this.tablename = tablename;
        @SuppressWarnings("rawtypes")
		Vector columnNames = new Vector();
        @SuppressWarnings("rawtypes")
		Vector data = new Vector();

        try
        {
            //  Connect to an Access Database
        	
        	try {
    			// Load the JDBC driver
    			Class.forName(driverName);
    			// Create a connection to the database
    			System.out.println("for name done");
    			connection = (Connection) DriverManager.getConnection(uri,
    					username, password);
    			System.out.print("connection done");
    		} catch (ClassNotFoundException e) {
    			// Could not find the database driver

    			System.out.println("ClassNotFoundException : " + e.getMessage());

    		} catch (SQLException e) {
    			// Could not connect to the database
    			System.out.println(e.getMessage());

    		}

            //  Read data from a table

            String sql = "Select * from "+tablename+" ";
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

            //  Get column names

            for (int i = 1; i <= columns; i++)
            {
                columnNames.addElement( md.getColumnName(i) );
            }

            //  Get row data

            while (rs.next())
            {
                @SuppressWarnings("rawtypes")
				Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++)
                {
                    row.addElement( rs.getObject(i) );
                }

                data.addElement( row );
            }

            rs.close();
            stmt.close();
            connection.close();
        }
        catch(Exception e)
        {
            System.out.println( e );
        }

        //  Create table with database data

        JTable table = new JTable(data, columnNames)
        {
            @SuppressWarnings("rawtypes")
			public Class getColumnClass(int column)
            {
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

   
}